package data;

import java.io.*;
import java.util.*;
import databaseproject.Interface;
import databaseproject.Products;
import business.*;

public class ProductIO
{
    
    /*
    public static Product getProduct(String code, String filepath)
    {
        try
        {
            File file = new File(filepath);
            BufferedReader in = 
                new BufferedReader(
                new FileReader(file));

            String line = in.readLine();
            while (line != null)
            {
                StringTokenizer t = new StringTokenizer(line, "|");
                String productCode = t.nextToken();
                if (code.equalsIgnoreCase(productCode))
                {
                    String description = t.nextToken();
                    double price = Double.parseDouble(t.nextToken());
                    Product p = new Product();
                    p.setCode(code);
                    p.setDescription(description);
                    p.setPrice(price);
                    in.close();
                    return p;
                }
                line = in.readLine();
            }
            in.close();
            return null;
        }
        catch(IOException e)
        {
            e.printStackTrace();
            return null;
        }
    }
     * 
     * */

    
    public static Products getProduct(int code)
    {
       ArrayList< Products > results = null;
       
       Products p = new Products();
            try{
            //out.println("<html> <body>hello</body></html>");
           databaseproject.Interface result = new databaseproject.Interface();
            //results = connect().getProduct(code);
            
            p.setName(result.getProduct(code).get(0).getName());
            p.setPrice(result.getProduct(code).get(0).getPrice());
            p.setImage_link(result.getProduct(code).get(0).getImage_link());
            
            //request.setAttribute("results", results);
            
            
          //RequestDispatcher dispatcher=getServletContext().getRequestDispatcher("/softwareCatalog.jsp");
          //dispatcher.forward(request,response);
        
        //ArrayList<Product> products = new ArrayList<Product>();
            }catch(Exception e){
                System.out.println(e);
            }
        return p; 
        
    }
    
   
    /*

    public static ArrayList<Product> getProducts(String filepath)
    {
        ArrayList<Product> products = new ArrayList<Product>();
        File file = new File(filepath);
        try
        {
            BufferedReader in = 
                new BufferedReader(
                new FileReader(file));

            String line = in.readLine();
            while (line != null)
            {
                StringTokenizer t = new StringTokenizer(line, "|");
                String code = t.nextToken();
                String description = t.nextToken();
                String priceAsString = t.nextToken();
                double price = Double.parseDouble(priceAsString);
                Product p = new Product();
                p.setCode(code);
                p.setDescription(description);
                p.setPrice(price);
                products.add(p);
                line = in.readLine();
            }
            in.close();
            return products;
        }
        catch(IOException e)
        {
            e.printStackTrace();
            return null;
        }
      
      }
     * 
     * */
     
    
}